//
//  ShareView.swift
//  ShareTest1
//
//  Created by leeqb on 12/11/15.
//  Copyright © 2015 powerlee. All rights reserved.
//

import UIKit

var g_shareView: ShareView!

class ShareView: UIView, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    var parentVC: UIViewController!
    let imgs = ["ic_share_wx_friend", "ic_share_wx_circle", "ic_share_qq", "ic_share_sina_wb", "ic_share_sms"]
    let titles = ["微信好友", "微信朋友圈", "QQ好友", "新浪微博", "短信分享"]
    
    @IBOutlet var cntView: UIView!
    @IBOutlet weak var bottomView: UIView!
    @IBOutlet weak var collectionView: UICollectionView!
    @IBOutlet weak var bottomSpace: NSLayoutConstraint!
    
    // MARK: - Initialization
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.initSelf()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        self.initSelf()
    }
    
    private func initSelf() {
        NSBundle.mainBundle().loadNibNamed("ShareView", owner: self, options: nil)
        self.addSubview(cntView)
        cntView.frame.size = self.frame.size
        
        bottomView.hidden = true
        collectionView.registerClass(ShareViewCell.classForCoder(), forCellWithReuseIdentifier: "ShareCell")
    }
    
    // MARK: - Class Methods
    class func show() {
        let keyWindow = UIApplication.sharedApplication().keyWindow
        if keyWindow != nil {
            g_shareView = ShareView(frame: keyWindow!.frame)
            keyWindow!.addSubview(g_shareView)
            g_shareView.show()
        }
    }
    
    class func dismiss() {
        g_shareView.dismiss()
    }
    
    // MARK: - Public Methods
    func show() {
        bottomView.hidden = false
        bottomSpace.constant = -bottomView.frame.height
        self.cntView.layoutIfNeeded()
        UIView.animateWithDuration(0.3, animations: { () -> Void in
            self.bottomSpace.constant = 0
            self.cntView.layoutIfNeeded()
            }) { (Bool) -> Void in
        }
    }
    
    func dismiss() {
        self.cntView.layoutIfNeeded()
        UIView.animateWithDuration(0.3, animations: { () -> Void in
            self.bottomSpace.constant = -self.bottomView.frame.height
            self.cntView.layoutIfNeeded()
            }) { (Bool) -> Void in
                g_shareView.removeFromSuperview()
        }
    }
    
    // MARK: - Gesture Handle
    @IBAction func tabHandler(sender: UITapGestureRecognizer) {
        self.dismiss()
    }
    
    // MARK: - UICollectionViewDelegateFlowLayout
    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
        return CGSize(width: SCREEN_WIDTH / 3, height: 80)
    }
    
    // MARK: - UICollectionViewDataSource
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return imgs.count
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("ShareCell", forIndexPath: indexPath) as! ShareViewCell
        
        cell.imageView.image = UIImage(named: imgs[indexPath.row])
        cell.textLabel.text = titles[indexPath.row]
        
        return cell
    }
    
    // MARK: - UICollectionViewDelegate
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        self.dismiss()
        switch indexPath.row {
        case 0: // 微信好友
            ShareHelper.wxFriendShare(UIApplication.sharedApplication().keyWindow?.rootViewController, content: "test", image: UIImage(named: "ic_share_qq"))
            break
        case 1: // 微信朋友圈
            ShareHelper.wxCircleShare(UIApplication.sharedApplication().keyWindow?.rootViewController, content: "test", image: UIImage(named: "ic_share_qq"))
            break
        case 2: // QQ好友
            ShareHelper.qqShare(UIApplication.sharedApplication().keyWindow?.rootViewController, content: "test", image: UIImage(named: "ic_share_qq"))
            break
        case 3: // 新浪分享
            ShareHelper.sinaShare(UIApplication.sharedApplication().keyWindow?.rootViewController, content: "test", image: UIImage(named: "ic_share_qq"))
            break
        case 4: // 短信分享
            ShareHelper.smsShare(UIApplication.sharedApplication().keyWindow?.rootViewController, content: "test")
            break
        default:
            break
        }
    }
}
